import request from '../utils/request'
import apis from '@/api'
import store from '../store'
import axios from 'axios'
import config from './config'

const Install = {
  vm: {},
  install (Vue, options) {
    if (this.installed) {
      return
    }
    this.installed = true

    Vue.prototype.$hasauth = function (authorities) {
      let role = store.state.user.authorities.includes('ROLE_super_admin')
      if(role){
        return true
      } else {
        if (!authorities) {
          return false
        }
        return authorities.split(',').some(item => {
          return store.state.user.authorities.includes('ACTION_' + item)
        })
      }
    }

    // api接口列表
    Vue.prototype.$apis = apis
    Vue.prototype.$axios = axios
    Vue.prototype.$config = config



    // http请求封装
    Vue.prototype.$http = {
      /**
       * post 加密请求
       * @param code 请求路径值
       * @param data 请求数据
       * @param cryptoEnabled
       * @param repeatEnabled
       * @param desensitizeFlag
       * @returns {Promise<any>}
       */
      postCrypto: function(code, data, cryptoEnabled = true, repeatEnabled, desensitizeFlag) {
        return this.post(code, data, cryptoEnabled, repeatEnabled, desensitizeFlag)
      },
      /**
       * post请求
       * @param code 请求路径值
       * @param data 请求数据
       * @param cryptoEnabled
       * @param repeatEnabled
       * @param desensitizeFlag
       * @returns {Promise<any>}
       */
      post: function (code, data, cryptoEnabled, repeatEnabled, desensitizeFlag) {
        let headers = {
          'func-code': code,
          'x-desensitize-flag': desensitizeFlag + '',
          cryptoEnabled: cryptoEnabled + '',
          repeatEnabled: repeatEnabled + ''
        }
        return new Promise((resolve, reject) => {
          request.post(code, data, { headers }).then(res => {
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        })
      },

      getCrypto: function(code, params, cryptoEnabled = true, repeatEnabled, desensitizeFlag) {
        return this.get(code, params, cryptoEnabled, repeatEnabled, desensitizeFlag)
      },

      /**
       * get 请求
       * @param code   请求路径
       * @param params 请求数据
       * @param cryptoEnabled
       * @param repeatEnabled
       * @param desensitizeFlag
       * @returns {Promise<any>}
       */
      get: function (code, params = {}, cryptoEnabled, repeatEnabled, desensitizeFlag) {
        return new Promise((resolve, reject) => {
          let headers = {
            'func-code': code,
            'x-desensitize-flag': desensitizeFlag + '',
            cryptoEnabled: cryptoEnabled + '',
            repeatEnabled: repeatEnabled + ''
          }
          return request({
            method: 'get',
            url: code,
            params: params,
            config: { headers },
          });

          // request.get(url, params, { params }).then(res => {
          //   resolve(res)
          // }).catch(function (err) {
          //   reject(err)
          // })
        })
      },

      /**
       * 带询问操作
       * @param url  请求地址
       * @param data 请求数据
       * @param options {content:'提示内容'}
       * @param context 当前vue实例this
       * @returns {Promise}
       */
      confirm: function (url, data = {}, options = {}, context, method = 'post') {
        return new Promise((resolve, reject) => {
          const content = options.content || '确定执行该操作吗?'
          context.$modal.confirm(content).then(()=> {
            if(method=='get'){
              request.get(url, { params: data }).then(res => {
                resolve(res)
              }).catch(function (err) {
                reject(err)
              })
            } else {
              request.post(url, data).then(res => {
                resolve(res)
              }).catch(function (err) {
                reject(err)
              })
            }
          }).catch(() => {})
        })
      }
    }
  }
}

export default Install
